﻿<template>
  <div>
    <a-card :bordered="false" :bodyStyle="tstyle">
      <div class="table-page-search-wrapper" v-if="hasPerm('Voucher:page')">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="8" :sm="24">
              <a-form-item label="日期">
                <a-date-picker
                  style="width: 100%"
                  placeholder="请选择日期"
                  v-model="queryParam.billdateDate"
                  @change="onChangebilldate"
                />
              </a-form-item>
            </a-col>

            <a-col :md="8" :sm="24">
              <span class="table-page-search-submitButtons">
                
                <a-button type="primary" @click="$refs.table.refresh(true)" >查询</a-button>
                <a-button type="primary" @click="VoucherDownload()" :loading="loadingState">数据下载</a-button>
                <a-button type="primary" @click="VoucherUpload()" :loading="loadingState">数据上传</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <a-card :bordered="false">
      <s-table
        ref="table"
        :columns="columns"
        :data="loadData"
        :alert="true"
        :rowKey="(record) => record.id"
        :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
        :showPagination="true"
        :scroll="{ x: 1500, y: 600 }"
        :bordered="true"   >
        <template class="table-operator" slot="operator" v-if="hasPerm('Voucher:add')">
          <a-button v-show="false" type="primary" v-if="hasPerm('Voucher:add')" icon="plus" @click="$refs.addForm.add()"
            >新增凭证记录</a-button
          >
        </template>
        <span slot="action" slot-scope="text, record">
          <a v-show="false" v-if="hasPerm('Voucher:edit')" @click="$refs.editForm.edit(record)">编辑</a>
          <a-divider type="vertical" v-if="hasPerm('Voucher:edit') & hasPerm('Voucher:delete')" />
          <a-popconfirm
            v-if="hasPerm('Voucher:delete')"
            placement="topRight"
            title="确认删除？"
            @confirm="() => VoucherDelete(record)"
          >
            <a>删除</a>
          </a-popconfirm>
        </span>
      </s-table>
      <add-form ref="addForm" @ok="handleOk" />
      <edit-form ref="editForm" @ok="handleOk" />
    </a-card>
  </div>
</template>
<script>
import { STable } from '@/components'
import moment from 'moment'
import { VoucherPage, VoucherDelete, VoucherUpload, SyncHISVoucher } from '@/api/modular/main/VoucherManage'
import addForm from './addForm.vue'
import editForm from './editForm.vue'
export default {
  components: {
    STable,
    addForm,
    editForm,
  },
  data() {
    return {
      advanced: false, // 高级搜索 展开/关闭
      queryParam: {},
      scroll: 'y',
      columns: [
        {
          title: '业务分类',
          align: 'center',
          dataIndex: 'classtype',
          width: 80,
          fixed: 'left',
        },
        {
          title: '单据类型 ',
          align: 'center',
          dataIndex: 'pk_busitype',
          width: 80,
        },
        {
          title: 'ID',
          align: 'center',
          dataIndex: 'billid',
          width: 80,
          ellipsis: true,
        },
        {
          title: '公司编码',
          align: 'center',
          dataIndex: 'pk_corp',
          width: 80,
        },
        {
          title: '日期',
          align: 'center',
          dataIndex: 'billdate',
          width: 120,
          ellipsis: true,
        },
        {
          title: '项目名称',
          align: 'center',
          dataIndex: 'projectname',
          width: 120,
        },
        {
          title: '科室名称',
          align: 'center',
          dataIndex: 'deptname',
          width: 200,
        },
        {
          title: '收款方式',
          align: 'center',
          dataIndex: 'intypename',
          width: 120,
        },
        {
          title: '仓库编码',
          align: 'center',
          dataIndex: 'storecode',
          width: 80,
        },
        {
          title: '仓库名称',
          align: 'center',
          dataIndex: 'storename',
          width: 80,
        },
        {
          title: '业务类型',
          align: 'center',
          dataIndex: 'histype',
          width: 120,
        },
        {
          title: '供应商名称',
          align: 'center',
          dataIndex: 'suppliername',
          width: 200,
        },
        {
          title: '生成凭证',
          align: 'center',
          dataIndex: 'istovoucher',
          width: 80,
        },
        {
          title: '是否退货',
          align: 'center',
          dataIndex: 'backmoneyflag',
          width: 80,
        },
        {
          title: '金额',
          align: 'center',
          dataIndex: 'money',
          width: 80,
          fixed: 'right',
        },
        {
          title: '备注',
          align: 'center',
          dataIndex: 'memo',
          width: 80,
        },
      ],
      tstyle: { 'padding-bottom': '0px', 'margin-bottom': '10px' },
      // 加载数据方法 必须为 Promise 对象
      loadData: (parameter) => {
        return VoucherPage(Object.assign(parameter, this.switchingDate())).then((res) => {
          return res.data
        })
      },
      selectedRowKeys: [],
      selectedRows: [],
      loadingState: false,
    }
  },
  created() {
    if (this.hasPerm('Voucher:edit') || this.hasPerm('Voucher:delete')) {
      this.columns.push({
        title: '操作',
        width: '120px',
        dataIndex: 'action',
        scopedSlots: { customRender: 'action' },
        fixed: 'right',
        align: 'center',
      })
    }
  },
  methods: {
    moment,
    /**
     * 查询参数组装
     */
    switchingDate() {
      const queryParambilldate = this.queryParam.billdateDate
      if (queryParambilldate != null) {
        this.queryParam.billdate = moment(queryParambilldate).format('YYYY-MM-DD')
        if (queryParambilldate.length < 1) {
          delete this.queryParam.billdate
        }
      }
      const obj = JSON.parse(JSON.stringify(this.queryParam))
      return obj
    },
    VoucherDelete(record) {
      VoucherDelete(record).then((res) => {
        if (res.success) {
          this.$message.success('删除成功')
          this.$refs.table.refresh()
        } else {
          this.$message.error('删除失败') // + res.message
        }
      })
    },
    beginLoading() {
      this.loadingState = true;

    },
    endLoading() {
      this.loadingState = false;
    },
    VoucherUpload() {
      let billdate = this.queryParam
      console.log(JSON.parse(JSON.stringify(this.queryParam)))
      if (billdate == null) {
        this.$message.error('请选择同步日期')
        return false
      }
      this.beginLoading();
      VoucherUpload(billdate).then((res) => {
        if (res.success) {
          this.$message.success('同步成功')
          this.$refs.table.refresh()
          this.endLoading();
        } else {
          this.endLoading();
          this.$message.error('同步失败' + res.message) //
        }
      })
      
    },
    VoucherDownload() {
      const queryParambilldate = this.queryParam.billdateDate
      if (queryParambilldate != null) {
        this.queryParam.billdate = moment(queryParambilldate).format('YYYY-MM-DD')
        if (queryParambilldate.length < 1) {
          delete this.queryParam.billdate
        }
      } else {
        this.$message.error('请选择同步日期')
        return false
      }
      this.beginLoading();
      SyncHISVoucher(this.queryParam).then((res) => {
        if (res.success) {
          this.$message.success('同步HIS业务凭证成功')
          this.$refs.table.refresh();
          this.endLoading();
        } else {
          this.$message.error('同步失败' + res.message) //
          this.endLoading();
        }
      })
      
    },
    toggleAdvanced() {
      this.advanced = !this.advanced
    },
    onChangebilldate(date, dateString) {
      this.billdateDateString = dateString
    },
    handleOk() {
      this.$refs.table.refresh()
    },
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
  },
}
</script>
<style lang="less">
.table-operator {
  margin-bottom: 18px;
}
button {
  margin-right: 8px;
}
</style>
